package com.ruoyi.yijing.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.yijing.domain.YjGuaYao;
import com.ruoyi.yijing.service.IYjGuaiYaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.yijing.mapper.YjGuaiMapper;
import com.ruoyi.yijing.domain.YjGua;
import com.ruoyi.yijing.service.IYjGuaiService;

/**
 * 卦基本信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-22
 */
@Service
public class YjGuaiServiceImpl implements IYjGuaiService 
{
    @Autowired
    private YjGuaiMapper yjGuaiMapper;

    @Autowired
    private IYjGuaiYaoService guaiYaoService;

    /**
     * 查询卦基本信息
     * 
     * @param guaiId 卦基本信息主键
     * @return 卦基本信息
     */
    @Override
    public YjGua selectYjGuaiByGuaiId(Integer guaiId)
    {
        YjGua guai = yjGuaiMapper.selectYjGuaiByGuaiId(guaiId);
        if (guai != null){
            YjGuaYao yao = new YjGuaYao();
            yao.setGuaId(guaiId);
            guai.setYaoList(guaiYaoService.selectYjGuaiYaoList(yao));
        }
        return guai;
    }

    /**
     * 查询卦基本信息列表
     * 
     * @param yjGuai 卦基本信息
     * @return 卦基本信息
     */
    @Override
    public List<YjGua> selectYjGuaiList(YjGua yjGuai)
    {
        return yjGuaiMapper.selectYjGuaiList(yjGuai);
    }

    @Override
    public List<YjGua> selectGuaList() {
        return yjGuaiMapper.selectGuaList();
    }

    /**
     * 新增卦基本信息
     * 
     * @param yjGuai 卦基本信息
     * @return 结果
     */
    @Override
    public int insertYjGuai(YjGua yjGuai)
    {
        yjGuai.setCreateBy(SecurityUtils.getUsername());
        yjGuai.setCreateTime(new Date());
        return yjGuaiMapper.insertYjGuai(yjGuai);
    }

    /**
     * 修改卦基本信息
     * 
     * @param yjGuai 卦基本信息
     * @return 结果
     */
    @Override
    public int updateYjGuai(YjGua yjGuai)
    {
        yjGuai.setUpdateBy(SecurityUtils.getUsername());
        yjGuai.setUpdateTime(new Date());
        if (yjGuaiMapper.updateYjGuai(yjGuai)>0){
            List<YjGuaYao> yaoList = yjGuai.getYaoList();
            for (YjGuaYao yao:yaoList) {
                if (yao.getYaoId() == null){
                    yao.setCreateBy(SecurityUtils.getUsername());
                    yao.setCreateTime(new Date());
                    guaiYaoService.insertYjGuaiYao(yao);
                }else{
                    yao.setUpdateBy(SecurityUtils.getUsername());
                    yao.setUpdateTime(new Date());
                    guaiYaoService.updateYjGuaiYao(yao);
                }
            }
            return 1;
        }
        return 0;
    }

    /**
     * 批量删除卦基本信息
     * 
     * @param guaiIds 需要删除的卦基本信息主键
     * @return 结果
     */
    @Override
    public int deleteYjGuaiByGuaiIds(Integer[] guaiIds)
    {
        return yjGuaiMapper.deleteYjGuaiByGuaiIds(guaiIds);
    }

    /**
     * 删除卦基本信息信息
     * 
     * @param guaiId 卦基本信息主键
     * @return 结果
     */
    @Override
    public int deleteYjGuaiByGuaiId(Integer guaiId)
    {
        return yjGuaiMapper.deleteYjGuaiByGuaiId(guaiId);
    }
}
